package com.graduation.cdm.funactivity.view;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.graduation.cdm.funactivity.MyApplication;
import com.graduation.cdm.funactivity.R;
import com.graduation.cdm.funactivity.bean.ActivityBean;
import com.graduation.cdm.funactivity.util.CommonAdapter;
import com.graduation.cdm.funactivity.util.ViewHolder;
import com.graduation.cdm.funactivity.util.xUtilsImageUtils;
import com.graduation.cdm.funactivity.view.activityview.OperatePublishActivity;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.graduation.cdm.funactivity.util.DateUtils.longTosmallstring;
import static com.graduation.cdm.funactivity.util.GlobalConstant.preUrl;

public class SearchActivity extends AppCompatActivity implements View.OnClickListener {

    private ImageView search_back;
    private EditText et_search;
    private TextView tv_search;
    private PullToRefreshListView lv_search;

    private int pageCount = 1;
    private int page = 1;
    CommonAdapter<ActivityBean.Activity> adapter = null;
    List<ActivityBean.Activity> activityLists = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);

        initView();

    }

    private void initView(){
        search_back = ((ImageView) findViewById(R.id.search_back));
        et_search = ((EditText) findViewById(R.id.et_search));
        tv_search = ((TextView) findViewById(R.id.tv_search));
        lv_search = ((PullToRefreshListView) findViewById(R.id.lv_search));

        search_back.setOnClickListener(this);
        tv_search.setOnClickListener(this);

        lv_search.setMode(PullToRefreshBase.Mode.PULL_FROM_END);
        lv_search.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ActivityBean.Activity activityDetail = activityLists.get(position - 1);
                Intent intent = new Intent(getApplicationContext(), OperatePublishActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("activityInfo",activityDetail);
                //System.out.println("============"+activityList.get(position));
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
        lv_search.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
            @Override
            public void onRefresh(PullToRefreshBase<ListView> refreshView) {
                page++;
                getActivityByheme();
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.search_back:
                finish();
                break;
            case R.id.tv_search:
                getActivityByheme();
                break;
        }
    }

    private void getActivityByheme(){
        String url = preUrl +"activity/getByTheme";
        StringRequest stringRequest=new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                System.out.println("onResponse="+s.trim());
                //CircularProgress.setVisibility(View.GONE);
                Gson gson = new Gson();
                ActivityBean activityBean = gson.fromJson(s, ActivityBean.class);
                pageCount = Integer.parseInt(activityBean.pageSum);
                if (page == 1) {
                    activityLists.clear();
                }
                if (page <= pageCount) {
                    activityLists.addAll(activityBean.activityList);
                }else {
                    Toast.makeText(getApplicationContext(), "已经是最后一页了", Toast.LENGTH_SHORT).show();
                }

                lv_search.onRefreshComplete();
                System.out.println(activityLists);
                initData(activityLists);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                System.out.println("onErrorResponse="+volleyError.toString());
            }
        }){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String,String> map=new HashMap<>();
                map.put("pageNow",page+"");
                map.put("pageSize",5+"");
                map.put("activityTheme",et_search.getText().toString());
                return map;
            }
        };
        MyApplication.getRequestQueue().add(stringRequest);
    }

    private void initData(List<ActivityBean.Activity> activityLists){
        if (adapter == null) {
            adapter = new CommonAdapter<ActivityBean.Activity>(getApplicationContext().getApplicationContext(), activityLists, R.layout.tuijian_item) {
                @Override
                public void convert(ViewHolder viewHolder, ActivityBean.Activity activity, int position) {
                    TextView tv_createtime = viewHolder.getViewById(R.id.tv_createtime);
                    TextView tv_label = viewHolder.getViewById(R.id.tv_label);
                    TextView tv_theme = viewHolder.getViewById(R.id.tv_theme);
                    ImageView iv_photo = viewHolder.getViewById(R.id.iv_photo);

                    tv_createtime.setText(longTosmallstring(activity.activityBegintime));
                    tv_label.setText(activity.activityLabel);
                    tv_theme.setText(activity.activityTheme);
                    xUtilsImageUtils.displayHome(iv_photo,preUrl+activity.activityImgurl);
                }
            };
            lv_search.setAdapter(adapter);
        }else {
            adapter.notifyDataSetChanged();
        }
        lv_search.onRefreshComplete();

    }
}
